<template>
  <div class="app-container">
    <div class="filter-container" style="padding-bottom:20px;">
      <el-input v-model="listQuery.name" placeholder="名称 封装 项目 产品" style="width: 400px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-date-picker
        v-model="listQuery.datetimeStart"
        type="datetime"
        placeholder="选择日期时间">
      </el-date-picker>
      <span class="demonstration">-</span>
      <el-date-picker
        v-model="listQuery.datetimeEnd"
        type="datetime"
        placeholder="选择日期时间">
      </el-date-picker>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-document">
        导出excel
      </el-button>
    </div>

    <div style="margin-top: 20px">
      <el-button v-waves class="filter-item" type="danger" icon="el-icon-delete" @click="handleDeleteAll">
        清空
      </el-button>
      <el-button v-waves class="filter-item" type="danger" icon="el-icon-delete" @click="handleDeleteSelected">
        删除选中
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-check" @click="handleCreateOrderAll">
        提交入库申请(全部)
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-check" @click="handleCreateOrderSelected">
        提交入库申请(选中)
      </el-button>
      <el-input v-model="note" placeholder="备注信息" style="width: 400px;"/>
    </div>

    <el-table
      ref="multipleTable"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
      style="width: 100%;"
      @selection-change="handleSelectionChange"
      >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="添加时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.created_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="项目" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.project }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.product }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.stock }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需要入库数" width="210" align="center">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.count" size="small" :step="1" step-strictly></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="210" align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>


<script>
import { electronicInCartFind, electronicInCartDelete, electronicInOrderCreate } from '@/api/electronic'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'find',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      total: 10,
      listQuery: {
        name: null,
        page: 1,
        limit: 20,
        datetimeStart: '',
        datetimeEnd: ''
      },
      note: '',
      multipleSelection:[],
      deletes: {
        electronics: null
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      electronicInCartFind(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        });
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDeleteAll() {
      this.$confirm('此操作将清空所有元器件订单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          electronicInCartDelete(JSON.stringify(this.list)).then(response => {
            this.$message({
              type: 'success',
              message: response.message
            });
            this.handleFilter()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleDeleteSelected() {
      this.$confirm('此操作将删除所选元器件订单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          electronicInCartDelete(JSON.stringify(this.multipleSelection)).then(response => {
            this.$message({
              type: 'success',
              message: response.message
            });
            this.handleFilter()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleDeleteRow(row) {
      var electronics = []
      electronics.push(row)

      this.$confirm('此操作将删除该元器件订单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          electronicInCartDelete(JSON.stringify(electronics)).then(response => {
            this.$message({
              type: 'success',
              message: response.message
            });
            this.handleFilter()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleCreateOrderAll(){
      electronicInOrderCreate(JSON.stringify(this.list), this.note).then(response => {
        this.handleFilter()
        this.$confirm(response.message, '提示', {
          confirmButtonText: '查询详情',
          cancelButtonText: '取消',
          type: 'success'
        }).then(() => {
          console.log("查询详情")
        })
      })
    },
    handleCreateOrderSelected(){
      electronicInOrderCreate(JSON.stringify(this.multipleSelection),this.note).then(response => {
        this.handleFilter()
        this.$confirm(response.message, '提示', {
          confirmButtonText: '查询详情',
          cancelButtonText: '取消',
          type: 'success'
        }).then(() => {
          console.log("查询详情")
        })
      })
    }
  }
}
</script>
